'''
Created on Jun 20, 2012

@author: Shashank.Ashtikar
'''

class Counter:
    def __init__(self, low, high):
        self.current = low
        self.high = high
        
    def __iter__(self):
        return self
    
    def next(self):
        if self.current > self.high:
            raise StopIteration
        else:
            self.current += 1
            return self.current - 1
    def __str__(self):
        return '<Iterator Counter object>'
        
class Even:
    """
    This class will generate even number within a given range.
    """
    def __init__(self, low, high):
        self.low = low
        self.high = high
        self.current = low
        
    def __iter__(self):
        return self
    
    def next(self):
        self.current += 1
        while self.current % 2 != 0:
            self.current += 1
        
        if  self.current > self.high:
            raise StopIteration
        return self.current
    def __str__(self):
        return "<Even number generator, Low=%d, Max=%d, Current number %d>"%(self.low, self.high, self.current)
    
class Odd:
    """
    This class will generate even number within given range.
    """
    
    def __init__(self, low, high):
        self.low = low
        self.high = high
        self.current = low
        
    def __iter__(self):
        return self
    
    def next(self):
        self.current += 1
        while self.current % 2 == 0:
            self.current += 1
            
        if self.current > self.high:
            raise StopIteration
        
        return self.current
    def __str__(self):
        return "<Odd number generator, low=%d, Max=%d, Current number=%d>"%(self.low, self.high, self.current)
    
class Prime:
    """
    This class will generate prime number in given range.
    """
    def __init__(self, low, max):
        self.low = low
        self.current = low
        self.high = max
        
    def __iter__(self):
        return self
    
    def next(self):
        self.current += 1
        while not self.is_prime(self.current):
            if self.current > self.high:
                raise StopIteration
            self.current += 1
            
        return self.current
    
    @staticmethod
    def is_prime(number):
        """
        This staticmethod checks if passed number is a prime number.
        """
        result = True
        start = 2
        while start < number:
            if number % start == 0:
                result = False
                break
            start += 1
        return result
    
    def __str__(self):
        return "<Prime number generator, low=%d, Max=%d, Current=%d>"%(self.low, 
                                                                       self.high, 
                                                                       self.current)

if __name__ == '__main__':
    for x in Counter(10, 20):
        print x
    
    print Even(10, 20)
    for num in Even(0, 20):
        print num
        
    print Odd(0, 20)
    for num in Odd(0, 20):
        print num
        
    print Prime(0, 20)
    for num in Prime(0, 20):
        print num
    print dir(Prime)
